<?php

/*
	info: 编辑器文件上传
	date: 2011-12-01
*/

require dirname(__FILE__).'/../include/common.inc.php';
require WEBFILEPATH.'/include/upload.class.php';
require WEBFILEPATH.'/editor/JSON.php';

$tempsavepath = WEBFILEPATH.'/upfiles';
$tempshowpath = WEBURLPATH.'/upfiles';
$webrequest['upfrom'] = isset($webrequest['upfrom']) ? trim($webrequest['upfrom']) : '';
switch ($webrequest['upfrom'])
{
	case 'webinfo':
			$tempsavepath .= '/webinfo';
			$tempshowpath .= '/webinfo';
			break;
	case 'company':
			$tempFoldername = '/company/'.global_FormatTime(0, 'Ym');
			$tempsavepath .= $tempFoldername;
			$tempshowpath .= $tempFoldername;
			break;
	case 'litpic':
			$tempFoldername = '/litpic/'.global_FormatTime(0, 'Ym');
			$tempsavepath .= $tempFoldername;
			$tempshowpath .= $tempFoldername;
			break;
	case 'news':
			$tempFoldername = '/news/'.global_FormatTime(0, 'Ym');
			$tempsavepath .= $tempFoldername;
			$tempshowpath .= $tempFoldername;
			break;
	case 'coupon':
			$tempFoldername = '/coupon/'.global_FormatTime(0, 'Ym');
			$tempsavepath .= $tempFoldername;
			$tempshowpath .= $tempFoldername;
			break;
	case 'integral':
			$tempFoldername = '/integral/'.global_FormatTime(0, 'Ym');
			$tempsavepath .= $tempFoldername;
			$tempshowpath .= $tempFoldername;
			break;
	case 'brand':
			$tempFoldername = '/brand/'.global_FormatTime(0, 'Ym');
			$tempsavepath .= $tempFoldername;
			$tempshowpath .= $tempFoldername;
			break;
}

$tempupconfig = array('type'=>array('gif', 'jpg', 'jpeg', 'png', 'bmp'), 'size'=>2097152);

if ($webrequest['upfrom'] == 'litpic') {
	$msg = '';
	if (isset($webupfile['litpic'])) {
		$tempupload = new Upload('litpic', $tempupconfig);
		if (!$tempupload->CheckFile()) {
			$msg = "<script language='javascript'>
				parent.document.getElementById('uploadwait').style.display = 'none';
				alert('". $tempupload->errorMsg ."');
			</script>";
			echo $msg;
			exit;
		}
		$tempupload->SaveFile($tempsavepath);
		if (empty($tempupload->fileList)) {
			$msg = "<script language='javascript'>
				parent.document.getElementById('uploadwait').style.display = 'none';
				alert('上传失败，请重试。');
			</script>";
			echo $msg;
			exit;
		}

		$msg = "<script language='javascript'>
					parent.document.getElementById('uploadwait').style.display = 'none';
					window.open('".WEBURLPATH."/editor/imagecut.php?f=picname&isupload=yes&file=/upfiles".$tempFoldername. '/' .$tempupload->fileList[0]['name']."', 'popUpImagesWin', 'scrollbars=yes,resizable=yes,statebar=no,width=800,height=600,left=150, top=50');
				</script>";
	}
	echo $msg;
	exit();
} else {
	if (isset($webupfile['imgFile'])) {
		$tempupload = new Upload('imgFile', $tempupconfig);
		if (!$tempupload->CheckFile())
		{
			editor_ReturnMsg(1, $tempupload->errorMsg);
		}
		$tempupload->SaveFile($tempsavepath);
		if (empty($tempupload->fileList))
		{
			editor_ReturnMsg(1, '上传失败，请重试。');
		}
		editor_ReturnMsg(0, $tempshowpath.'/'.$tempupload->fileList[0]['name']);
	}
}



function editor_ReturnMsg($intError, $strInfo)
{
	global $webconfig;
	header("Content-type: text/html; charset={$webconfig['webcharset']}");
	$json = new Services_JSON();
	if ($intError > 0)
	{
		echo $json->encode(array('error' => 1, 'message' => $strInfo));
	}
	else
	{
		echo $json->encode(array('error' => 0, 'url' => $strInfo));
	}
	exit();
}

?>